﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using ClassLibrary;
using ClassLibrary.Entity;
using ClassLibrary.DAO;
using System.IO;

public partial class Admin_ucControl_ucThemSanPham : System.Web.UI.UserControl
{
    TblItemDAO itemDAO = new TblItemDAO(Global.CONNECTION_STRING);
    TblTypeDAO typeDAO = new TblTypeDAO(Global.CONNECTION_STRING);

    protected void Page_Load(object sender, EventArgs e)
    {
        string id = Request.QueryString["id"];
        if (!IsPostBack)
        {
            LoadType();
            if (id != null)
            {
                LoadDetail();
                LoadImgItemByID();
            }           
        }
    }

    public void LoadType()
    {
        drType.DataSource = typeDAO.GetAllType();
        drType.DataTextField = "TypeName";
        drType.DataValueField = "IDType";
        drType.DataBind();
    }
    private void LoadImgItemByID()
    {
        try
        {
            string id = Request.QueryString["id"];
            grdImgItems.DataSource = itemDAO.GetImgItems(Global.ConvertToInt(id));
            grdImgItems.DataBind();
        }
        catch (Exception ex)
        {
            Session[Global.SESSION_ERROR] = ex;
            Response.Redirect("~/Error.aspx", true);
        }
    }

    private void LoadDetail()
    {
        LoadType();
        string id = Request.QueryString["id"];
        if (id != null)
        {
            TblItem item = itemDAO.GetItemByID(int.Parse(id));
            txtDescription.Content = Server.HtmlDecode(item.Description);
            txtItemName.Text = item.ItemName;
            txtPrice.Text = Convert.ToDouble(item.Price).ToString();
            txtSoLuong.Text = Convert.ToInt32(item.Quantity).ToString();
            drStatus.SelectedValue = Convert.ToInt32(item.Status).ToString();
            drType.SelectedValue = Convert.ToInt32(item.IDType).ToString();
        
        }
    }
    protected void btAdd_Click(object sender, EventArgs e)
    {
        TblItem item = new TblItem();
        item.ItemName = txtItemName.Text;
        item.Description = Server.HtmlDecode(txtDescription.Content);
        item.Price = float.Parse(txtPrice.Text);
        item.Quantity = int.Parse(txtSoLuong.Text);
        item.Status = int.Parse(drStatus.SelectedValue);
        item.IDType = int.Parse(drType.SelectedValue);

        item.ItemID = 0;
        itemDAO.InsUpdItem(item, 1);

    }
    protected void btnAddImage_Click(object sender, EventArgs e)
    {

        string id = Request.QueryString["id"];
        bool flag = false;
        try
        {

            string path = Server.MapPath("~\\Admin\\img\\Items\\");
            string filename = id + "_" + upImage.FileName;

            TblImage imgItem = new TblImage();
            // lblMessage.Text = path;
            if (File.Exists(path + "\\" + filename))
            {
                filename = DateTime.Now.ToString("mm") + "_" + DateTime.Now.ToString("ss") + "_" + filename;
            }
            else
            {
                filename = DateTime.Now.ToString("mm") + "_" + DateTime.Now.ToString("ss") + "_" + filename;
            }
            upImage.SaveAs(path + "\\" + filename);
            imgItem.ImageURL = filename;
            imgItem.ItemID = Convert.ToInt32(id);
          
            itemDAO.InsImageItem(imgItem, 1);
            
            flag = true;
        }
        catch (Exception ex)
        {
            Session[Global.SESSION_ERROR] = ex;
            Response.Redirect(Global.ERROR_PAGE_ADMIN);
        }
        if (flag)
        {
            if (id == null || id == "")
                Response.Redirect("~/Admin/ThemSanPham.aspx?id=" + id);
            else
                Response.Redirect("~/Admin/ThemSanPham.aspx?id=" + id);
        }
    }
    protected void grdImgItems_RowDataBound(object sender, GridViewRowEventArgs e)
    {

    }

    protected void btnDelImg_Click(object sender, ImageClickEventArgs e)
    {
    }
    protected void btnReset_Click(object sender, EventArgs e)
    {
        txtDescription.Content = string.Empty;
        txtItemName.Text = string.Empty;
        txtPrice.Text = string.Empty;
        txtSoLuong.Text = string.Empty;        
    }
}
